<template>
  <div class="app-container">
    <el-row :gutter="15">
      <div class="orderNo">{{ project.projectName }}</div>
    </el-row>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="投诉类型" prop="complaintType">
            <el-select v-model="queryParams.complaintType" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_complaint" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="处理状态" prop="handleStatus">
            <el-select v-model="queryParams.handleStatus" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in handleStatusOptions" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="受理人" prop="assignStaffName">
            <el-input v-model="queryParams.assignStaffName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="处理人" prop="handleName">
            <el-input v-model="queryParams.handleName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="complaintsList" border>
      <el-table-column align="center" label="投诉类型" prop="complaintType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_complaint" :value="scope.row.complaintType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="投诉人" prop="projectAccountName"/>
      <el-table-column align="center" label="投诉时间" prop="complaintTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.complaintTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="投诉描述" prop="complaintDesc"/>
      <el-table-column align="center" label="处理状态" prop="handleStatus">
        <template slot-scope="scope">
          <dict-tag :options="handleStatusOptions" :value="scope.row.handleStatus"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="受理人" prop="assignStaffName"/>
      <el-table-column align="center" label="组别" prop="groupName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_groups" :value="scope.row.groupName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="处理人" prop="handleStaffName"/>
      <el-table-column align="center" label="处理时间" prop="handleTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-if="[1, 2].includes(scope.row.handleStatus)" v-hasPermi="['cld:complaints:finish']" size="mini"
                     type="text" @click="handleFinish(scope.row)">完成
          </el-button>
          <el-button size="mini" type="text"
                     @click="handleUpdate(scope.row, 'detail')">详情
          </el-button>
          <el-button v-if="[1,2].includes(scope.row.handleStatus)" v-hasPermi="['cld:complaints:reply']" size="mini"
                     type="text" @click="handleUpdate(scope.row, 'reply')">回复
          </el-button>
          <el-button v-if="[0].includes(scope.row.handleStatus)" v-hasPermi="['cld:complaints:revocation']" size="mini"
                     type="text" @click="handleRevocation(scope.row)">撤销
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改客诉与处置对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body title="投诉记录" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-card v-for="item in form.detailsList" :key="item" class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ form.projectAccountName }}(客户)</span>
            <span style="float: right; padding: 3px 0" type="text">{{ form.complaintTime }}</span>
          </div>
          <div style="margin-top: 10px;">
            {{ form.complaintDesc }}
          </div>
          <div v-if="form.complaintImgs" style="margin-top: 10px;">
            <image-preview v-for="item in form.complaintImgs.split(',')" :key="item" :height="100"
                           :src="item" :width="100" style="margin-right: 10px;"/>
          </div>
        </el-card>
        <el-card v-for="item in form.replyList" :key="item.complanintsReplyId">
          <div slot="header" class="clearfix">
            <span> {{ item.replyStaffName }}(({{
                item.replyUserType == 1 ? item.replyStaffPostName : (item.replyUserType
                ==
                2 ? '系统' : '客户')
              }}))</span>
            <span style="float: right; padding: 3px 0" type="text">{{ item.replyTime }}</span>
          </div>
          <div style="margin-top: 10px;">
            {{ item.replyContent }}
          </div>
          <div v-if="item.replyImgs" style="margin-top: 10px;">
            <image-preview v-for="item in item.replyImgs.split(',')" :key="item" :height="100" :src="item"
                           :width="100" style="margin-right: 10px;"/>
          </div>
        </el-card>
        <el-form-item v-if="type == 'reply'" label="回复内容" prop="replyContent">
          <el-input v-model="form.replyContent" :rows="4" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="type == 'reply'" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addComplaintsReply, delComplaints, getComplaints, listComplaints, updateComplaints} from "@/api/cld/complaints";
import {handleStatusOptions} from '@/assets/staticDict'
import {getProject} from '@/api/cld/project'

export default {
  name: "Complaints",
  dicts: ['cld_project_complaint', 'cld_project_groups'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客诉与处置表格数据
      complaintsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: this.$route.params.projectId,
        complaintType: null,
        handleStatus: null,
        assignStaffName: null,
        handleName: null
      },
      // 表单参数
      form: {},
      replyForm: {},
      // 表单校验
      rules: {
        replyContent: [
          {required: true, message: "回复内容不能为空", trigger: "blur"},
        ],
      },
      // 处理状态
      handleStatusOptions: handleStatusOptions,
      type: 'reply',
      // 项目信息
      project: {}
    };
  },
  created() {
    this.getList();
    this.getProjectInfo();
  },
  methods: {
    /** 查询客诉与处置列表 */
    getList() {
      this.loading = true;
      listComplaints(this.queryParams).then(response => {
        this.complaintsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询项目信息
    getProjectInfo() {
      getProject(this.$route.params.projectId).then(res => {
        this.project = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.replyForm = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.reset();
      const complaintsId = row.complaintsId || this.ids
      getComplaints(complaintsId).then(response => {
        this.type = type;
        this.form = response.data;
        this.open = true;
        this.title = "回复记录";
      });
    },
    // 完成
    handleFinish(row) {
      this.$modal.confirm('是否设为已完成？').then(function () {
        let form = {
          complaintsId: row.complaintsId,
          handleStatus: 4
        }
        return updateComplaints(form);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    },
    // 撤销
    handleRevocation(row) {
      let form = {
        ...row
      }
      this.$prompt('', '撤销描述', {
        center: true,
        inputType: 'textarea',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        form.revokeText = value;
        form.handleStatus = 5;
        updateComplaints(form).then(response => {
          this.$modal.msgSuccess("操作成功");
          this.open = false;
          this.getList();
        });
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.replyForm.replyUserType = 2;
          this.replyForm.replyContent = this.form.replyContent;
          this.replyForm.complanintsId = this.form.complaintsId;
          addComplaintsReply(this.replyForm).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const complaintsIds = row.complaintsId || this.ids;
      this.$modal.confirm('是否确认删除客诉与处置编号为"' + complaintsIds + '"的数据项？').then(function () {
        return delComplaints(complaintsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
<style scoped>
::v-deep .el-timeline-item__timestamp.is-top {
  text-align: right;
  padding-right: 10px;
}

.userName {
  position: relative;
  bottom: 0px;
}

::v-deep .el-timeline-item__content {
  margin-top: -25px;
}

::v-deep .el-card__body {
  padding: 0px 5px 5px 10px;
}
::v-deep .el-card {
  margin-bottom: 5px;
}
</style>
